#!/usr/bin/env python
# encoding: utf-8
# @author: liusir
# @file: testcase_data_utils.py
# @time: 2022/11/13 9:10 AM
import os
from utlis import excel_utils
from utlis.config_utils import local_config
from utlis.mysql_utils import MysqlUtils

current_path = os.path.dirname(os.path.abspath(__file__))
testcase_file_path = os.path.join( current_path,'..','test_data','testcase_infos.xlsx' )
sql_str = """
select case_info.case_id as '测试用例编号',case_info.case_name as '测试用例名称',case_info.is_run as '用例执行',case_step_info.case_step_id as '用例步骤',requests_info.requests_name as '接口名称',requests_info.requests_type as '请求方式',requests_info.requests_header as '请求头部信息',requests_info.requests_url as '请求地址',requests_info.requests_url_params as '请求参数(get)',requests_info.requests_post_data '请求参数(post)',case_step_info.get_value_type as '取值方式',case_step_info.get_value_variable as '取值变量',case_step_info.get_value_code as '取值代码',case_step_info.excepted_result_type as '断言类型',case_step_info.excepted_result as '期望结果'
from case_info,case_step_info,requests_info
where case_info.case_id = case_step_info.case_id and case_step_info.requests_id = requests_info.requests_id and case_info.is_run = '是'
order by case_info.case_id,case_step_info.case_step_id;
"""

class TestcaseDataUtils:
    def __init__(self):
        # self.test_data = excel_utils.ExcelUtils(testcase_file_path,"Sheet1").get_all_excel_data()
        self.test_data = MysqlUtils(local_config.MYSQL_SERVER_IP,
                                    local_config.MYSQL_USER,
                                    local_config.MYSQL_PASSWORD).get_all_data(sql_str)
    def convert_testcase_data_to_dict(self):
        test_case_dict = {}
        for row_data in self.test_data:
            if row_data['用例执行'] == '是':
                test_case_dict.setdefault(row_data['测试用例编号'], []).append(row_data)
        return test_case_dict
    def convert_testcase_data_to_list_unittest(self):
        test_case_list = []
        for key,value in self.convert_testcase_data_to_dict().items():
            case_info_dict = {}
            case_info_dict['case_id'] = key
            case_info_dict['case_step'] = value
            test_case_list.append( case_info_dict )
        return test_case_list
    def convert_testcase_data_to_pytest(self):
        test_case_list = self.convert_testcase_data_to_list_unittest()
        case_key_list = list(test_case_list[0].keys())
        case_key_str = ",".join(case_key_list)
        test_data_list = []
        for case_data in test_case_list:
            case_value = tuple(case_data.values())
            test_data_list.append(case_value)
        return (case_key_str,test_data_list)


if __name__=="__main__":
    data_dict = TestcaseDataUtils().convert_testcase_data_to_dict()
    print( data_dict )
    data_list = TestcaseDataUtils().convert_testcase_data_to_list_unittest()
    print( data_list )
    data = TestcaseDataUtils().convert_testcase_data_to_pytest()
    print( data )


